//打印、刻录任务
import { createSlice } from '@reduxjs/toolkit'
import { secretOptions } from "../../utils/constant";
const taskSlice = createSlice({
    name: 'task',
    initialState: {
        printTask: [],
        burnTask: []
    },
    reducers: {
        addPrintTask(state: any, action: any) {
            const { payload } = action
            const index = state.printTask.findIndex((r: any) => r.taskId === payload.taskId);
            if (index > -1) {
                const item = state.printTask[index];
                item.currentPage = payload.currentPage;
                item.currentPartNum = payload.currentPartNum;
                const a = item.currentPage + (item.currentPartNum - 1) * item.totalPage
                const b = item.totalPage * item.partNum
                const progress = Number.isNaN((a / b) * 100) ? 0 : (a / b) * 100;
                item.progress = progress
                state.printTask[index].currentPage = payload.currentPage;
            } else {
                const item = payload;
                const a = item.currentPage + (item.currentPartNum - 1) * item.totalPage
                const b = item.totalPage * item.partNum
                const progress = Number.isNaN((a / b) * 100) ? 0 : (a / b) * 100;
                item.progress = progress
                if (progress < 100) {
                    state.printTask.push(item);
                }
            }
        },
        removeTaskArray(state, action) {
            const { payload } = action
            const index = state.printTask.findIndex((r: any) => r.taskId === payload);
            if (index > -1) {
                state.printTask.splice(index, 1);
            }
        },
        addBurnTask(state: any, action: any) {
            const { payload } = action
            const index = state.burnTask.findIndex(
                (item: any) => item.replyId === payload.replyId
            );
            if (index > -1) {
                const item = state.burnTask[index]
                item.progress = payload.progress
            } else {
                state.burnTask.push({
                    ...payload.taskInfo,
                    replyId: payload.replyId,
                    secretLabel: secretOptions.find(
                        (item) => item.value === payload.taskInfo?.secret
                    )?.label,
                    progress: payload.progress,
                    fileType: "",
                    status: payload.progress === 100 ? 1 : 0,
                })
            }
        },
        removeBurnTask(state) {
            state.burnTask = []
        }
    }
})
export const { addPrintTask, removeTaskArray, addBurnTask, removeBurnTask } = taskSlice.actions
export default taskSlice.reducer